/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.report.web;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.book.entity.BkType;
import com.thinkgem.jeesite.modules.book.service.BookTypeService;
import com.thinkgem.jeesite.modules.bookset.entity.BkPlace;
import com.thinkgem.jeesite.modules.bookset.service.BookPlaceService;
import com.thinkgem.jeesite.modules.report.entity.GeneralLedgerDetail;
import com.thinkgem.jeesite.modules.report.entity.LedgerSearch;
import com.thinkgem.jeesite.modules.report.service.BookReportService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 流程个人任务相关Controller
 *
 * @author ThinkGem
 * @version 2013-11-03
 */
@Controller
@RequestMapping(value = "${adminPath}/report")
public class BookReportController extends BaseController{
    @Autowired
    private BookReportService bookReportService;
    @Autowired
    private BookPlaceService bookPlaceService;
    @Autowired
    private BookTypeService bookTypeService;

    @ModelAttribute
    public LedgerSearch get(){
        return new LedgerSearch();
    }

    @RequiresPermissions("book:store:view")
    @RequestMapping("toLedgerDetail")
    public String generalLedgerDetail(){
        return "modules/report/generalLedgerDetail";
    }
    /**
     * 细分总括帐页面
     *
     * @param ledgerSearch
     * @param model
     * @return
     */
    @RequiresPermissions("book:store:view")
    @RequestMapping("generalLedgerDetail")
    public String generalLedgerDetail(LedgerSearch ledgerSearch, Model model){
        List<BkType> bkTypes = bookTypeService.getbyPid("0");
        model.addAttribute("bkTypes", bkTypes);

        Page<BkPlace> bkPlaces = bookPlaceService.getAll(new Page<BkPlace>(),new BkPlace());
        model.addAttribute("bkPlaces", bkPlaces.getList());

        Map<String, Double> count = bookReportService.getPurchaseLedgerDetailCount(ledgerSearch);

        List<GeneralLedgerDetail> generalledger = bookReportService.getPurchaseLedgerDetail(ledgerSearch, false);

        model.addAttribute("count", count.get("count"));
        model.addAttribute("total", count.get("total"));
        model.addAttribute("generalledger", generalledger);

        model.addAttribute("ledgerSearch", ledgerSearch);
        return "modules/report/generalLedgerDetail";
    }

    /**
     * 导出总括帐Excel
     * @param ledgerSearch
     * @param response
     * @return
     */
    @RequiresPermissions("book:store:view")
    @RequestMapping(value = "exportGeneralLedgerDetail")
    public String exportGeneralLedgerDetail(LedgerSearch ledgerSearch, HttpServletResponse response){
        List<GeneralLedgerDetail> generalledgers = bookReportService.getPurchaseLedgerDetail(ledgerSearch, true);
        try{
            String fileName = "总括明细账" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            new ExportExcel("总括明细账", GeneralLedgerDetail.class)
                    .setDataList(generalledgers)
                    .write(response, fileName)
                    .dispose();
            return null;
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return "redirect:" + adminPath + "/report/generalLedgerDetail?repage";
    }
}